#include <iostream>
#include <algorithm>
#include <iterator>
using namespace std;

bool needSwap(char str[], int i, int j) {
	int k;
	for (k = i; k < j && (str[k] != str[j]); ++k) 
		;
	return k == j;
}

void Range(char str[], int len, int pos) {
	if (pos == len-1) {
		std::copy(str, str+len, ostream_iterator<char>(cout, " "));
		cout << endl;
	}
	for (int i = pos ; i < len; i++) {
		if (needSwap(str, pos, i)) {
			std::swap(str[pos], str[i]);
			Range(str, len, pos+1);
			std::swap(str[pos], str[i]);
		}
	}

}

int main() {
	char str[] = "122";
	Range(str, 3, 0);
	return 0;
}
